home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / src / binutils.252 / gas / expr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-28  |  30.8 KB  |  1,114 lines

  1. /* expr.c -operands, expressions-
  2.    Copyright (C) 1987, 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
  3.  
  4.    This file is part of GAS, the GNU Assembler.
  5.  
  6.    GAS is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2, or (at your option)
  9.    any later version.
  10.  
  11.    GAS is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with GAS; see the file COPYING.  If not, write to
  18.    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  19.  
  20. /*
  21.  * This is really a branch office of as-read.c. I split it out to clearly
  22.  * distinguish the world of expressions from the world of statements.
  23.  * (It also gives smaller files to re-compile.)
  24.  * Here, "operand"s are of expressions, not instructions.
  25.  */
  26.  
  27. #include <ctype.h>
  28. #include <string.h>
  29.  
  30. #include "as.h"
  31.  
  32. #include "obstack.h"
  33.  
  34. static void floating_constant PARAMS ((expressionS * expressionP));
  35. static void integer_constant PARAMS ((int radix, expressionS * expressionP));
  36. static void clean_up_expression PARAMS ((expressionS * expressionP));
  37.  
  38. extern const char EXP_CHARS[], FLT_CHARS[];
  39.  
  40. /* Build a dummy symbol to hold a complex expression.  This is how we
  41.    build expressions up out of other expressions.  The symbol is put
  42.    into the fake section expr_section.  */
  43.  
  44. symbolS *
  45. make_expr_symbol (expressionP)
  46.      expressionS *expressionP;
  47. {
  48.   const char *fake;
  49.   symbolS *symbolP;
  50.  
  51.   if (expressionP->X_op == O_symbol
  52.       && expressionP->X_add_number == 0)
  53.     return expressionP->X_add_symbol;
  54.  
  55.   /* FIXME: This should be something which decode_local_label_name
  56.      will handle.  */
  57.   fake = FAKE_LABEL_NAME;
  58.  
  59.   /* Putting constant symbols in absolute_section rather than
  60.      expr_section is convenient for the old a.out code, for which
  61.      S_GET_SEGMENT does not always retrieve the value put in by
  62.      S_SET_SEGMENT.  */
  63.   symbolP = symbol_create (fake,
  64.                (expressionP->X_op == O_constant
  65.                 ? absolute_section
  66.                 : expr_section),
  67.                0, &zero_address_frag);
  68.   symbolP->sy_value = *expressionP;
  69.  
  70.   if (expressionP->X_op == O_constant)
  71.     resolve_symbol_value (symbolP);
  72.  
  73.   return symbolP;
  74. }
  75.  
  76. /*
  77.  * Build any floating-point literal here.
  78.  * Also build any bignum literal here.
  79.  */
  80.  
  81. /* Seems atof_machine can backscan through generic_bignum and hit whatever
  82.    happens to be loaded before it in memory.  And its way too complicated
  83.    for me to fix right.  Thus a hack.  JF:  Just make generic_bignum bigger,
  84.    and never write into the early words, thus they'll always be zero.
  85.    I hate Dean's floating-point code.  Bleh.  */
  86. LITTLENUM_TYPE generic_bignum[SIZE_OF_LARGE_NUMBER + 6];
  87. FLONUM_TYPE generic_floating_point_number =
  88. {
  89.   &generic_bignum[6],        /* low (JF: Was 0) */
  90.   &generic_bignum[SIZE_OF_LARGE_NUMBER + 6 - 1], /* high JF: (added +6) */
  91.   0,                /* leader */
  92.   0,                /* exponent */
  93.   0                /* sign */
  94. };
  95. /* If nonzero, we've been asked to assemble nan, +inf or -inf */
  96. int generic_floating_point_magic;
  97.  
  98. static void
  99. floating_constant (expressionP)
  100.      expressionS *expressionP;
  101. {
  102.   /* input_line_pointer->*/
  103.   /* floating-point constant. */
  104.   int error_code;
  105.  
  106.   error_code = atof_generic (&input_line_pointer, ".", EXP_CHARS,
  107.                  &generic_floating_point_number);
  108.  
  109.   if (error_code)
  110.     {
  111.       if (error_code == ERROR_EXPONENT_OVERFLOW)
  112.     {
  113.       as_bad ("bad floating-point constant: exponent overflow, probably assembling junk");
  114.     }
  115.       else
  116.     {
  117.       as_bad ("bad floating-point constant: unknown error code=%d.", error_code);
  118.     }
  119.     }
  120.   expressionP->X_op = O_big;
  121.   /* input_line_pointer->just after constant, */
  122.   /* which may point to whitespace. */
  123.   expressionP->X_add_number = -1;
  124. }
  125.  
  126. static void
  127. integer_constant (radix, expressionP)
  128.      int radix;
  129.      expressionS *expressionP;
  130. {
  131.   char *start;        /* start of number. */
  132.   char c;
  133.  
  134.   valueT number;    /* offset or (absolute) value */
  135.   short int digit;    /* value of next digit in current radix */
  136.   short int maxdig = 0;/* highest permitted digit value. */
  137.   int too_many_digits = 0;    /* if we see >= this number of */
  138.   char *name;        /* points to name of symbol */
  139.   symbolS *symbolP;    /* points to symbol */
  140.  
  141.   int small;            /* true if fits in 32 bits. */
  142.   extern const char hex_value[]; /* in hex_value.c */
  143.  
  144.   /* May be bignum, or may fit in 32 bits. */
  145.   /* Most numbers fit into 32 bits, and we want this case to be fast.
  146.      so we pretend it will fit into 32 bits.  If, after making up a 32
  147.      bit number, we realise that we have scanned more digits than
  148.      comfortably fit into 32 bits, we re-scan the digits coding them
  149.      into a bignum.  For decimal and octal numbers we are
  150.      conservative: Some numbers may be assumed bignums when in fact
  151.      they do fit into 32 bits.  Numbers of any radix can have excess
  152.      leading zeros: We strive to recognise this and cast them back
  153.      into 32 bits.  We must check that the bignum really is more than
  154.      32 bits, and change it back to a 32-bit number if it fits.  The
  155.      number we are looking for is expected to be positive, but if it
  156.      fits into 32 bits as an unsigned number, we let it be a 32-bit
  157.      number.  The cavalier approach is for speed in ordinary cases. */
  158.   /* This has been extended for 64 bits.  We blindly assume that if
  159.      you're compiling in 64-bit mode, the target is a 64-bit machine.
  160.      This should be cleaned up.  */
  161.  
  162. #ifdef BFD64
  163. #define valuesize 64
  164. #else /* includes non-bfd case, mostly */
  165. #define valuesize 32
  166. #endif
  167.  
  168.   switch (radix)
  169.     {
  170.     case 2:
  171.       maxdig = 2;
  172.       too_many_digits = valuesize + 1;
  173.       break;
  174.     case 8:
  175.       maxdig = radix = 8;
  176.       too_many_digits = (valuesize + 2) / 3 + 1;
  177.       break;
  178.     case 16:
  179.       maxdig = radix = 16;
  180.       too_many_digits = (valuesize + 3) / 4 + 1;
  181.       break;
  182.     case 10:
  183.       maxdig = radix = 10;
  184.       too_many_digits = (valuesize + 12) / 4; /* very rough */
  185.     }
  186. #undef valuesize
  187.   start = input_line_pointer;
  188.   c = *input_line_pointer++;
  189.   for (number = 0;
  190.        (digit = hex_value[(unsigned char) c]) < maxdig;
  191.        c = *input_line_pointer++)
  192.     {
  193.       number = number * radix + digit;
  194.     }
  195.   /* c contains character after number. */
  196.   /* input_line_pointer->char after c. */
  197.   small = (input_line_pointer - start - 1) < too_many_digits;
  198.   if (!small)
  199.     {
  200.       /*
  201.        * we saw a lot of digits. manufacture a bignum the hard way.
  202.        */
  203.       LITTLENUM_TYPE *leader;    /*->high order littlenum of the bignum. */
  204.       LITTLENUM_TYPE *pointer;    /*->littlenum we are frobbing now. */
  205.       long carry;
  206.  
  207.       leader = generic_bignum;
  208.       generic_bignum[0] = 0;
  209.       generic_bignum[1] = 0;
  210.       input_line_pointer = start;    /*->1st digit. */
  211.       c = *input_line_pointer++;
  212.       for (;
  213.        (carry = hex_value[(unsigned char) c]) < maxdig;
  214.        c = *input_line_pointer++)
  215.     {
  216.       for (pointer = generic_bignum;
  217.            pointer <= leader;
  218.            pointer++)
  219.         {
  220.           long work;
  221.  
  222.           work = carry + radix * *pointer;
  223.           *pointer = work & LITTLENUM_MASK;
  224.           carry = work >> LITTLENUM_NUMBER_OF_BITS;
  225.         }
  226.       if (carry)
  227.         {
  228.           if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1)
  229.         {
  230.           /* room to grow a longer bignum. */
  231.           *++leader = carry;
  232.         }
  233.         }
  234.     }
  235.       /* again, c is char after number, */
  236.       /* input_line_pointer->after c. */
  237.       know (LITTLENUM_NUMBER_OF_BITS == 16);
  238.       if (leader < generic_bignum + 2)
  239.     {
  240.       /* will fit into 32 bits. */
  241.       number =
  242.         ((generic_bignum[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
  243.         | (generic_bignum[0] & LITTLENUM_MASK);
  244.       small = 1;
  245.     }
  246.       else
  247.     {
  248.       number = leader - generic_bignum + 1;    /* number of littlenums in the bignum. */
  249.     }
  250.     }
  251.   if (small)
  252.     {
  253.       /*
  254.        * here with number, in correct radix. c is the next char.
  255.        * note that unlike un*x, we allow "011f" "0x9f" to
  256.        * both mean the same as the (conventional) "9f". this is simply easier
  257.        * than checking for strict canonical form. syntax sux!
  258.        */
  259.  
  260.       switch (c)
  261.     {
  262.  
  263. #ifdef LOCAL_LABELS_FB
  264.     case 'b':
  265.       {
  266.         /*
  267.          * backward ref to local label.
  268.          * because it is backward, expect it to be defined.
  269.          */
  270.         /* Construct a local label.  */
  271.         name = fb_label_name ((int) number, 0);
  272.  
  273.         /* seen before, or symbol is defined: ok */
  274.         symbolP = symbol_find (name);
  275.         if ((symbolP != NULL) && (S_IS_DEFINED (symbolP)))
  276.           {
  277.         /* local labels are never absolute. don't waste time
  278.            checking absoluteness. */
  279.         know (SEG_NORMAL (S_GET_SEGMENT (symbolP)));
  280.  
  281.         expressionP->X_op = O_symbol;
  282.         expressionP->X_add_symbol = symbolP;
  283.           }
  284.         else
  285.           {
  286.         /* either not seen or not defined. */
  287.         /* @@ Should print out the original string instead of
  288.            the parsed number.  */
  289.         as_bad ("backw. ref to unknown label \"%d:\", 0 assumed.",
  290.             (int) number);
  291.         expressionP->X_op = O_constant;
  292.           }
  293.  
  294.         expressionP->X_add_number = 0;
  295.         break;
  296.       }            /* case 'b' */
  297.  
  298.     case 'f':
  299.       {
  300.         /*
  301.          * forward reference. expect symbol to be undefined or
  302.          * unknown. undefined: seen it before. unknown: never seen
  303.          * it before.
  304.          * construct a local label name, then an undefined symbol.
  305.          * don't create a xseg frag for it: caller may do that.
  306.          * just return it as never seen before.
  307.          */
  308.         name = fb_label_name ((int) number, 1);
  309.         symbolP = symbol_find_or_make (name);
  310.         /* we have no need to check symbol properties. */
  311. #ifndef many_segments
  312.         /* since "know" puts its arg into a "string", we
  313.            can't have newlines in the argument.  */
  314.         know (S_GET_SEGMENT (symbolP) == undefined_section || S_GET_SEGMENT (symbolP) == text_section || S_GET_SEGMENT (symbolP) == data_section);
  315. #endif
  316.         expressionP->X_op = O_symbol;
  317.         expressionP->X_add_symbol = symbolP;
  318.         expressionP->X_add_number = 0;
  319.  
  320.         break;
  321.       }            /* case 'f' */
  322.  
  323. #endif /* LOCAL_LABELS_FB */
  324.  
  325. #ifdef LOCAL_LABELS_DOLLAR
  326.  
  327.     case '$':
  328.       {
  329.  
  330.         /* If the dollar label is *currently* defined, then this is just
  331.            another reference to it.  If it is not *currently* defined,
  332.            then this is a fresh instantiation of that number, so create
  333.            it.  */
  334.  
  335.         if (dollar_label_defined ((long) number))
  336.           {
  337.         name = dollar_label_name ((long) number, 0);
  338.         symbolP = symbol_find (name);
  339.         know (symbolP != NULL);
  340.           }
  341.         else
  342.           {
  343.         name = dollar_label_name ((long) number, 1);
  344.         symbolP = symbol_find_or_make (name);
  345.           }
  346.  
  347.         expressionP->X_op = O_symbol;
  348.         expressionP->X_add_symbol = symbolP;
  349.         expressionP->X_add_number = 0;
  350.  
  351.         break;
  352.       }            /* case '$' */
  353.  
  354. #endif /* LOCAL_LABELS_DOLLAR */
  355.  
  356.     default:
  357.       {
  358.         expressionP->X_op = O_constant;
  359.         expressionP->X_add_number = number;
  360.         input_line_pointer--;    /* restore following character. */
  361.         break;
  362.       }            /* really just a number */
  363.  
  364.     }            /* switch on char following the number */
  365.  
  366.     }
  367.   else
  368.     {
  369.       /* not a small number */
  370.       expressionP->X_op = O_big;
  371.       expressionP->X_add_number = number;    /* number of littlenums */
  372.       input_line_pointer--;    /*->char following number. */
  373.     }
  374. }
  375.  
  376.  
  377. /*
  378.  * Summary of operand().
  379.  *
  380.  * in:    Input_line_pointer points to 1st char of operand, which may
  381.  *    be a space.
  382.  *
  383.  * out:    A expressionS.
  384.  *    The operand may have been empty: in this case X_op == O_absent.
  385.  *    Input_line_pointer->(next non-blank) char after operand.
  386.  */
  387.  
  388. static segT
  389. operand (expressionP)
  390.      expressionS *expressionP;
  391. {
  392.   char c;
  393.   symbolS *symbolP;    /* points to symbol */
  394.   char *name;        /* points to name of symbol */
  395.   segT segment;
  396.  
  397.   /* All integers are regarded as unsigned unless they are negated.
  398.      This is because the only thing which cares whether a number is
  399.      unsigned is the code in emit_expr which extends constants into
  400.      bignums.  It should only sign extend negative numbers, so that
  401.      something like ``.quad 0x80000000'' is not sign extended even
  402.      though it appears negative if valueT is 32 bits.  */
  403.   expressionP->X_unsigned = 1;
  404.  
  405.   /* digits, assume it is a bignum. */
  406.  
  407.   SKIP_WHITESPACE ();        /* leading whitespace is part of operand. */
  408.   c = *input_line_pointer++;    /* input_line_pointer->past char in c. */
  409.  
  410.   switch (c)
  411.     {
  412. #ifdef MRI
  413.     case '%':
  414.       integer_constant (2, expressionP);
  415.       break;
  416.     case '@':
  417.       integer_constant (8, expressionP);
  418.       break;
  419.     case '$':
  420.       integer_constant (16, expressionP);
  421.       break;
  422. #endif
  423.     case '1':
  424.     case '2':
  425.     case '3':
  426.     case '4':
  427.     case '5':
  428.     case '6':
  429.     case '7':
  430.     case '8':
  431.     case '9':
  432.       input_line_pointer--;
  433.  
  434.       integer_constant (10, expressionP);
  435.       break;
  436.  
  437.     case '0':
  438.       /* non-decimal radix */
  439.  
  440.       c = *input_line_pointer;
  441.       switch (c)
  442.     {
  443.  
  444.     default:
  445.       if (c && strchr (FLT_CHARS, c))
  446.         {
  447.           input_line_pointer++;
  448.           floating_constant (expressionP);
  449.           expressionP->X_add_number = -(isupper (c) ? tolower (c) : c);
  450.         }
  451.       else
  452.         {
  453.           /* The string was only zero */
  454.           expressionP->X_op = O_constant;
  455.           expressionP->X_add_number = 0;
  456.         }
  457.  
  458.       break;
  459.  
  460.     case 'x':
  461.     case 'X':
  462.       input_line_pointer++;
  463.       integer_constant (16, expressionP);
  464.       break;
  465.  
  466.     case 'b':
  467. #ifdef LOCAL_LABELS_FB
  468.       switch (input_line_pointer[1])
  469.         {
  470.         case '+':
  471.         case '-':
  472.           /* If unambiguously a difference expression, treat it as
  473.          one by indicating a label; otherwise, it's always a
  474.          binary number.  */
  475.           {
  476.         char *cp = input_line_pointer + 1;
  477.         while (strchr ("0123456789", *++cp))
  478.           ;
  479.         if (*cp == 'b' || *cp == 'f')
  480.           goto is_0b_label;
  481.           }
  482.           goto is_0b_binary;
  483.         case '0':    case '1':
  484.           /* Some of our code elsewhere does permit digits greater
  485.          than the expected base; for consistency, do the same
  486.          here.  */
  487.         case '2':    case '3':    case '4':    case '5':
  488.         case '6':    case '7':    case '8':    case '9':
  489.           goto is_0b_binary;
  490.         case 0:
  491.           goto is_0b_label;
  492.         default:
  493.           goto is_0b_label;
  494.         }
  495.     is_0b_label:
  496.       input_line_pointer--;
  497.       integer_constant (10, expressionP);
  498.       break;
  499.     is_0b_binary:
  500. #endif
  501.     case 'B':
  502.       input_line_pointer++;
  503.       integer_constant (2, expressionP);
  504.       break;
  505.  
  506.     case '0':
  507.     case '1':
  508.     case '2':
  509.     case '3':
  510.     case '4':
  511.     case '5':
  512.     case '6':
  513.     case '7':
  514.       integer_constant (8, expressionP);
  515.       break;
  516.  
  517.     case 'f':
  518. #ifdef LOCAL_LABELS_FB
  519.       /* If it says "0f" and it could possibly be a floating point
  520.          number, make it one.  Otherwise, make it a local label,
  521.          and try to deal with parsing the rest later.  */
  522.       if (!input_line_pointer[1]
  523.           || (is_end_of_line[0xff & input_line_pointer[1]]))
  524.         goto is_0f_label;
  525.       {
  526.         char *cp = input_line_pointer + 1;
  527.         int r = atof_generic (&cp, ".", EXP_CHARS,
  528.                   &generic_floating_point_number);
  529.         switch (r)
  530.           {
  531.           case 0:
  532.           case ERROR_EXPONENT_OVERFLOW:
  533.         if (*cp == 'f' || *cp == 'b')
  534.           /* looks like a difference expression */
  535.           goto is_0f_label;
  536.         else
  537.           goto is_0f_float;
  538.           default:
  539.         as_fatal ("expr.c(operand): bad atof_generic return val %d",
  540.               r);
  541.           }
  542.       }
  543.  
  544.       /* Okay, now we've sorted it out.  We resume at one of these
  545.          two labels, depending on what we've decided we're probably
  546.          looking at.  */
  547.     is_0f_label:
  548.       input_line_pointer--;
  549.       integer_constant (10, expressionP);
  550.       break;
  551.  
  552.     is_0f_float:
  553.       /* fall through */
  554. #endif
  555.  
  556.     case 'd':
  557.     case 'D':
  558.     case 'F':
  559.     case 'r':
  560.     case 'e':
  561.     case 'E':
  562.     case 'g':
  563.     case 'G':
  564.       input_line_pointer++;
  565.       floating_constant (expressionP);
  566.       expressionP->X_add_number = -(isupper (c) ? tolower (c) : c);
  567.       break;
  568.  
  569. #ifdef LOCAL_LABELS_DOLLAR
  570.     case '$':
  571.       integer_constant (10, expressionP);
  572.       break;
  573. #endif
  574.     }
  575.  
  576.       break;
  577.  
  578.     case '(':
  579.     case '[':
  580.       /* didn't begin with digit & not a name */
  581.       segment = expression (expressionP);
  582.       /* Expression() will pass trailing whitespace */
  583.       if (c == '(' && *input_line_pointer++ != ')' ||
  584.       c == '[' && *input_line_pointer++ != ']') 
  585.     {
  586.       as_bad ("Missing ')' assumed");
  587.       input_line_pointer--;
  588.     }
  589.       /* here with input_line_pointer->char after "(...)" */
  590.       return segment;
  591.  
  592.     case '\'':
  593.       /* Warning: to conform to other people's assemblers NO ESCAPEMENT is
  594.      permitted for a single quote. The next character, parity errors and
  595.      all, is taken as the value of the operand. VERY KINKY.  */
  596.       expressionP->X_op = O_constant;
  597.       expressionP->X_add_number = *input_line_pointer++;
  598.       break;
  599.  
  600.     case '+':
  601.       (void) operand (expressionP);
  602.       break;
  603.  
  604.     case '~':
  605.     case '-':
  606.       {
  607.     operand (expressionP);
  608.     if (expressionP->X_op == O_constant)
  609.       {
  610.         /* input_line_pointer -> char after operand */
  611.         if (c == '-')
  612.           {
  613.         expressionP->X_add_number = - expressionP->X_add_number;
  614.         /* Notice: '-' may overflow: no warning is given. This is
  615.            compatible with other people's assemblers. Sigh.  */
  616.         expressionP->X_unsigned = 0;
  617.           }
  618.         else
  619.           expressionP->X_add_number = ~ expressionP->X_add_number;
  620.       }
  621.     else if (expressionP->X_op != O_illegal
  622.          && expressionP->X_op != O_absent)
  623.       {
  624.         expressionP->X_add_symbol = make_expr_symbol (expressionP);
  625.         if (c == '-')
  626.           expressionP->X_op = O_uminus;
  627.         else
  628.           expressionP->X_op = O_bit_not;
  629.         expressionP->X_add_number = 0;
  630.       }
  631.     else
  632.       as_warn ("Unary operator %c ignored because bad operand follows",
  633.            c);
  634.       }
  635.       break;
  636.  
  637.     case '.':
  638. #ifdef DOLLAR_DOT
  639.     case '$':
  640. #endif
  641.       if (!is_part_of_name (*input_line_pointer))
  642.     {
  643.       const char *fake;
  644.  
  645.       /* JF: '.' is pseudo symbol with value of current location
  646.          in current segment.  */
  647.       fake = FAKE_LABEL_NAME;
  648.       symbolP = symbol_new (fake,
  649.                 now_seg,
  650.                 (valueT) frag_now_fix (),
  651.                 frag_now);
  652.  
  653.       expressionP->X_op = O_symbol;
  654.       expressionP->X_add_symbol = symbolP;
  655.       expressionP->X_add_number = 0;
  656.       break;
  657.     }
  658.       else
  659.     {
  660.       goto isname;
  661.     }
  662.     case ',':
  663.     case '\n':
  664.     case '\0':
  665.     eol:
  666.       /* can't imagine any other kind of operand */
  667.       expressionP->X_op = O_absent;
  668.       input_line_pointer--;
  669.       md_operand (expressionP);
  670.       break;
  671.  
  672.     default:
  673.       if (is_end_of_line[(unsigned char) c])
  674.     goto eol;
  675.       if (is_name_beginner (c))    /* here if did not begin with a digit */
  676.     {
  677.       /*
  678.        * Identifier begins here.
  679.        * This is kludged for speed, so code is repeated.
  680.        */
  681.     isname:
  682.       name = --input_line_pointer;
  683.       c = get_symbol_end ();
  684.       symbolP = symbol_find_or_make (name);
  685.  
  686.       /* If we have an absolute symbol or a reg, then we know its
  687.          value now.  */
  688.       segment = S_GET_SEGMENT (symbolP);
  689.       if (segment == absolute_section)
  690.         {
  691.           expressionP->X_op = O_constant;
  692.           expressionP->X_add_number = S_GET_VALUE (symbolP);
  693.         }
  694.       else if (segment == reg_section)
  695.         {
  696.           expressionP->X_op = O_register;
  697.           expressionP->X_add_number = S_GET_VALUE (symbolP);
  698.         }
  699.       else
  700.         {
  701.           expressionP->X_op = O_symbol;
  702.           expressionP->X_add_symbol = symbolP;
  703.           expressionP->X_add_number = 0;
  704.         }
  705.       *input_line_pointer = c;
  706.     }
  707.       else
  708.     {
  709.       as_bad ("Bad expression");
  710.       expressionP->X_op = O_constant;
  711.       expressionP->X_add_number = 0;
  712.     }
  713.     }
  714.  
  715.   /*
  716.    * It is more 'efficient' to clean up the expressionS when they are created.
  717.    * Doing it here saves lines of code.
  718.    */
  719.   clean_up_expression (expressionP);
  720.   SKIP_WHITESPACE ();        /*->1st char after operand. */
  721.   know (*input_line_pointer != ' ');
  722.  
  723.   /* The PA port needs this information.  */
  724.   if (expressionP->X_add_symbol)
  725.     expressionP->X_add_symbol->sy_used = 1;
  726.  
  727.   switch (expressionP->X_op)
  728.     {
  729.     default:
  730.       return absolute_section;
  731.     case O_symbol:
  732.       return S_GET_SEGMENT (expressionP->X_add_symbol);
  733.     case O_register:
  734.       return reg_section;
  735.     }
  736. }                /* operand() */
  737.  
  738. /* Internal. Simplify a struct expression for use by expr() */
  739.  
  740. /*
  741.  * In:    address of a expressionS.
  742.  *    The X_op field of the expressionS may only take certain values.
  743.  *    Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
  744.  * Out:    expressionS may have been modified:
  745.  *    'foo-foo' symbol references cancelled to 0,
  746.  *        which changes X_op from O_subtract to O_constant.
  747.  *    Unused fields zeroed to help expr().
  748.  */
  749.  
  750. static void
  751. clean_up_expression (expressionP)
  752.      expressionS *expressionP;
  753. {
  754.   switch (expressionP->X_op)
  755.     {
  756.     case O_illegal:
  757.     case O_absent:
  758.       expressionP->X_add_number = 0;
  759.       /* Fall through.  */
  760.     case O_big:
  761.     case O_constant:
  762.     case O_register:
  763.       expressionP->X_add_symbol = NULL;
  764.       /* Fall through.  */
  765.     case O_symbol:
  766.     case O_uminus:
  767.     case O_bit_not:
  768.       expressionP->X_op_symbol = NULL;
  769.       break;
  770.     case O_subtract:
  771.       if (expressionP->X_op_symbol == expressionP->X_add_symbol
  772.       || ((expressionP->X_op_symbol->sy_frag
  773.            == expressionP->X_add_symbol->sy_frag)
  774.           && SEG_NORMAL (S_GET_SEGMENT (expressionP->X_add_symbol))
  775.           && (S_GET_VALUE (expressionP->X_op_symbol)
  776.           == S_GET_VALUE (expressionP->X_add_symbol))))
  777.     {
  778.       addressT diff = (S_GET_VALUE (expressionP->X_add_symbol)
  779.                - S_GET_VALUE (expressionP->X_op_symbol));
  780.  
  781.       expressionP->X_op = O_constant;
  782.       expressionP->X_add_symbol = NULL;
  783.       expressionP->X_op_symbol = NULL;
  784.       expressionP->X_add_number += diff;
  785.     }
  786.       break;
  787.     default:
  788.       break;
  789.     }
  790. }
  791.  
  792. /* Expression parser. */
  793.  
  794. /*
  795.  * We allow an empty expression, and just assume (absolute,0) silently.
  796.  * Unary operators and parenthetical expressions are treated as operands.
  797.  * As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
  798.  *
  799.  * We used to do a aho/ullman shift-reduce parser, but the logic got so
  800.  * warped that I flushed it and wrote a recursive-descent parser instead.
  801.  * Now things are stable, would anybody like to write a fast parser?
  802.  * Most expressions are either register (which does not even reach here)
  803.  * or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
  804.  * So I guess it doesn't really matter how inefficient more complex expressions
  805.  * are parsed.
  806.  *
  807.  * After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
  808.  * Also, we have consumed any leading or trailing spaces (operand does that)
  809.  * and done all intervening operators.
  810.  *
  811.  * This returns the segment of the result, which will be
  812.  * absolute_section or the segment of a symbol.
  813.  */
  814.  
  815. #undef __
  816. #define __ O_illegal
  817.  
  818. static const operatorT op_encoding[256] =
  819. {                /* maps ASCII->operators */
  820.  
  821.   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  822.   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  823.  
  824.   __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
  825.   __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
  826.   __, __, __, __, __, __, __, __,
  827.   __, __, __, __, O_left_shift, __, O_right_shift, __,
  828.   __, __, __, __, __, __, __, __,
  829.   __, __, __, __, __, __, __, __,
  830.   __, __, __, __, __, __, __, __,
  831.   __, __, __, __, __, __, O_bit_exclusive_or, __,
  832.   __, __, __, __, __, __, __, __,
  833.   __, __, __, __, __, __, __, __,
  834.   __, __, __, __, __, __, __, __,
  835.   __, __, __, __, O_bit_inclusive_or, __, __, __,
  836.  
  837.   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  838.   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  839.   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  840.   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  841.   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  842.   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  843.   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  844.   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
  845. };
  846.  
  847.  
  848. /*
  849.  *    Rank    Examples
  850.  *    0    operand, (expression)
  851.  *    1    + -
  852.  *    2    & ^ ! |
  853.  *    3    * / % << >>
  854.  *    4    unary - unary ~
  855.  */
  856. static const operator_rankT op_rank[] =
  857. {
  858.   0,    /* O_illegal */
  859.   0,    /* O_absent */
  860.   0,    /* O_constant */
  861.   0,    /* O_symbol */
  862.   0,    /* O_register */
  863.   0,    /* O_bit */
  864.   4,    /* O_uminus */
  865.   4,    /* O_bit_now */
  866.   3,    /* O_multiply */
  867.   3,    /* O_divide */
  868.   3,    /* O_modulus */
  869.   3,    /* O_left_shift */
  870.   3,    /* O_right_shift */
  871.   2,    /* O_bit_inclusive_or */
  872.   2,    /* O_bit_or_not */
  873.   2,    /* O_bit_exclusive_or */
  874.   2,    /* O_bit_and */
  875.   1,    /* O_add */
  876.   1,    /* O_subtract */
  877. };
  878.  
  879. segT
  880. expr (rank, resultP)
  881.      operator_rankT rank;    /* Larger # is higher rank. */
  882.      expressionS *resultP;    /* Deliver result here. */
  883. {
  884.   segT retval;
  885.   expressionS right;
  886.   operatorT op_left;
  887.   char c_left;        /* 1st operator character. */
  888.   operatorT op_right;
  889.   char c_right;
  890.  
  891.   know (rank >= 0);
  892.  
  893.   retval = operand (resultP);
  894.  
  895.   know (*input_line_pointer != ' ');    /* Operand() gobbles spaces. */
  896.  
  897.   c_left = *input_line_pointer;    /* Potential operator character. */
  898.   op_left = op_encoding[(unsigned char) c_left];
  899.   while (op_left != O_illegal && op_rank[(int) op_left] > rank)
  900.     {
  901.       segT rightseg;
  902.  
  903.       input_line_pointer++;    /*->after 1st character of operator. */
  904.       /* Operators "<<" and ">>" have 2 characters. */
  905.       if (*input_line_pointer == c_left && (c_left == '<' || c_left == '>'))
  906.     ++input_line_pointer;
  907.  
  908.       rightseg = expr (op_rank[(int) op_left], &right);
  909.       if (right.X_op == O_absent)
  910.     {
  911.       as_warn ("missing operand; zero assumed");
  912.       right.X_op = O_constant;
  913.       right.X_add_number = 0;
  914.       right.X_add_symbol = NULL;
  915.       right.X_op_symbol = NULL;
  916.     }
  917.  
  918.       know (*input_line_pointer != ' ');
  919.  
  920.       if (retval == undefined_section)
  921.     {
  922.       if (SEG_NORMAL (rightseg))
  923.         retval = rightseg;
  924.     }
  925.       else if (! SEG_NORMAL (retval))
  926.     retval = rightseg;
  927.       else if (SEG_NORMAL (rightseg)
  928.            && retval != rightseg
  929. #ifdef DIFF_EXPR_OK
  930.            && op_left != O_subtract
  931. #endif
  932.            )
  933.     as_bad ("operation combines symbols in different segments");
  934.  
  935.       c_right = *input_line_pointer;
  936.       op_right = op_encoding[(unsigned char) c_right];
  937.       if (*input_line_pointer == c_right && (c_right == '<' || c_right == '>'))
  938.     ++input_line_pointer;
  939.  
  940.       know (op_right == O_illegal || op_rank[(int) op_right] <= op_rank[(int) op_left]);
  941.       know ((int) op_left >= (int) O_multiply && (int) op_left <= (int) O_subtract);
  942.  
  943.       /* input_line_pointer->after right-hand quantity. */
  944.       /* left-hand quantity in resultP */
  945.       /* right-hand quantity in right. */
  946.       /* operator in op_left. */
  947.  
  948.       if (resultP->X_op == O_big)
  949.     {
  950.       as_warn ("left operand of %c is a %s; integer 0 assumed",
  951.            c_left, resultP->X_add_number > 0 ? "bignum" : "float");
  952.       resultP->X_op = O_constant;
  953.       resultP->X_add_number = 0;
  954.       resultP->X_add_symbol = NULL;
  955.       resultP->X_op_symbol = NULL;
  956.     }
  957.       if (right.X_op == O_big)
  958.     {
  959.       as_warn ("right operand of %c is a %s; integer 0 assumed",
  960.            c_left, right.X_add_number > 0 ? "bignum" : "float");
  961.       right.X_op = O_constant;
  962.       right.X_add_number = 0;
  963.       right.X_add_symbol = NULL;
  964.       right.X_op_symbol = NULL;
  965.     }
  966.  
  967.       /* Optimize common cases.  */
  968. #if 0
  969.       if (op_left == O_add && resultP->X_got_symbol)
  970.     {
  971.       /* XXX - kludge here to accomodate "_GLOBAL_OFFSET_TABLE + (x - y)"
  972.        * expressions: this only works for this special case, the
  973.        * _GLOBAL_OFFSET_TABLE thing *must* be the left operand, the whole
  974.        * expression is given the segment of right expression (always a DIFFERENCE,
  975.        * which should get resolved by fixup_segment())
  976.        */
  977.       resultP->X_op = right.X_op;
  978.       resultP->X_add_symbol = right.X_add_symbol;
  979.       resultP->X_op_symbol = right.X_op_symbol;
  980.     }
  981.       else
  982. #endif
  983.     if (op_left == O_add && right.X_op == O_constant)
  984.     {
  985.       /* X + constant.  */
  986.       resultP->X_add_number += right.X_add_number;
  987.     }
  988.       /* This case comes up in PIC code.  */
  989.       else if (op_left == O_subtract
  990.            && right.X_op == O_symbol
  991.            && resultP->X_op == O_symbol
  992.            && (right.X_add_symbol->sy_frag
  993.            == resultP->X_add_symbol->sy_frag)
  994.            && SEG_NORMAL (S_GET_SEGMENT (right.X_add_symbol)))
  995.  
  996.     {
  997.       resultP->X_add_number += right.X_add_number;
  998.       resultP->X_add_number += (S_GET_VALUE (resultP->X_add_symbol)
  999.                     - S_GET_VALUE (right.X_add_symbol));
  1000.       resultP->X_op = O_constant;
  1001.       resultP->X_add_symbol = 0;
  1002.     }
  1003.       else if (op_left == O_subtract && right.X_op == O_constant)
  1004.     {
  1005.       /* X - constant.  */
  1006.       resultP->X_add_number -= right.X_add_number;
  1007.     }
  1008.       else if (op_left == O_add && resultP->X_op == O_constant)
  1009.     {
  1010.       /* Constant + X.  */
  1011.       resultP->X_op = right.X_op;
  1012.       resultP->X_add_symbol = right.X_add_symbol;
  1013.       resultP->X_op_symbol = right.X_op_symbol;
  1014.       resultP->X_add_number += right.X_add_number;
  1015.       retval = rightseg;
  1016.     }
  1017.       else if (resultP->X_op == O_constant && right.X_op == O_constant)
  1018.     {
  1019.       /* Constant OP constant.  */
  1020.       offsetT v = right.X_add_number;
  1021.       if (v == 0 && (op_left == O_divide || op_left == O_modulus))
  1022.         {
  1023.           as_warn ("division by zero");
  1024.           v = 1;
  1025.         }
  1026.       switch (op_left)
  1027.         {
  1028.         case O_multiply:        resultP->X_add_number *= v; break;
  1029.         case O_divide:        resultP->X_add_number /= v; break;
  1030.         case O_modulus:        resultP->X_add_number %= v; break;
  1031.         case O_left_shift:        resultP->X_add_number <<= v; break;
  1032.         case O_right_shift:        resultP->X_add_number >>= v; break;
  1033.         case O_bit_inclusive_or:    resultP->X_add_number |= v; break;
  1034.         case O_bit_or_not:        resultP->X_add_number |= ~v; break;
  1035.         case O_bit_exclusive_or:    resultP->X_add_number ^= v; break;
  1036.         case O_bit_and:        resultP->X_add_number &= v; break;
  1037.         case O_add:            resultP->X_add_number += v; break;
  1038.         case O_subtract:        resultP->X_add_number -= v; break;
  1039.         default:            abort ();
  1040.         }
  1041.     }
  1042.       else if (resultP->X_op == O_symbol
  1043.            && right.X_op == O_symbol
  1044.            && (op_left == O_add
  1045.            || op_left == O_subtract
  1046.            || (resultP->X_add_number == 0
  1047.                && right.X_add_number == 0)))
  1048.     {
  1049.       /* Symbol OP symbol.  */
  1050.       resultP->X_op = op_left;
  1051.       resultP->X_op_symbol = right.X_add_symbol;
  1052.       if (op_left == O_add)
  1053.         resultP->X_add_number += right.X_add_number;
  1054.       else if (op_left == O_subtract)
  1055.         resultP->X_add_number -= right.X_add_number;
  1056.     }
  1057.       else
  1058.     {
  1059.       /* The general case.  */
  1060.       resultP->X_add_symbol = make_expr_symbol (resultP);
  1061.       resultP->X_op_symbol = make_expr_symbol (&right);
  1062.       resultP->X_op = op_left;
  1063.       resultP->X_add_number = 0;
  1064.       resultP->X_unsigned = 1;
  1065.     }
  1066.  
  1067.       op_left = op_right;
  1068.     }                /* While next operator is >= this rank. */
  1069.  
  1070.   /* The PA port needs this information.  */
  1071.   if (resultP->X_add_symbol)
  1072.     resultP->X_add_symbol->sy_used = 1;
  1073.  
  1074.   return resultP->X_op == O_constant ? absolute_section : retval;
  1075. }
  1076.  
  1077. /*
  1078.  *            get_symbol_end()
  1079.  *
  1080.  * This lives here because it belongs equally in expr.c & read.c.
  1081.  * Expr.c is just a branch office read.c anyway, and putting it
  1082.  * here lessens the crowd at read.c.
  1083.  *
  1084.  * Assume input_line_pointer is at start of symbol name.
  1085.  * Advance input_line_pointer past symbol name.
  1086.  * Turn that character into a '\0', returning its former value.
  1087.  * This allows a string compare (RMS wants symbol names to be strings)
  1088.  * of the symbol name.
  1089.  * There will always be a char following symbol name, because all good
  1090.  * lines end in end-of-line.
  1091.  */
  1092. char
  1093. get_symbol_end ()
  1094. {
  1095.   char c;
  1096.  
  1097.   while (is_part_of_name (c = *input_line_pointer++))
  1098.     ;
  1099.   *--input_line_pointer = 0;
  1100.   return (c);
  1101. }
  1102.  
  1103.  
  1104. unsigned int
  1105. get_single_number ()
  1106. {
  1107.   expressionS exp;
  1108.   operand (&exp);
  1109.   return exp.X_add_number;
  1110.  
  1111. }
  1112.  
  1113. /* end of expr.c */
  1114.